package org.rpgmapper.adapter;

import java.util.List;

import org.rpgmapper.R;
import org.rpgmapper.bo.Character;
import org.rpgmapper.database.RPGMapperOpenHelper;

import android.content.Context;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.rpgmapper.Main;
import org.rpgmapper.bo.Foe;

public class CharacterAdapter extends BaseAdapter {

    private String[] pcs;
    private List<Character> chars;
    private Context context;
    private LayoutInflater inflater;

    public CharacterAdapter(Context context, boolean isMonster) {
        this.context = context;
        RPGMapperOpenHelper helper = new RPGMapperOpenHelper(context);
        inflater = LayoutInflater.from(context);
        chars = isMonster ? helper.getAllMonsters() : helper.getAllHeroes();
        pcs = new String[chars.size()];
        int i = 0;
        for (Character character : chars) {
            pcs[i] = character.getName();
            i++;
        }
    }

    public int getCount() {
        return pcs.length;
    }

    public Object getItem(int position) {
        return pcs[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewGroup view;
        if (convertView == null) {
            view = (ViewGroup) inflater.inflate(R.layout.unknown_character, null);
        } else {
            view = (ViewGroup) convertView;
        }
        Character c = chars.get(position);
        if (c instanceof Foe) {
            switch ((int)c.getId()) {
                case 5:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.elf);
                    break;
                case 6:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.giant);
                    break;
                case 7:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.goblin);
                    break;
                case 8:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.god);
                    break;
                case 9:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.thug);
                    break;
                case 10:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.fly);
                    break;
                case 11:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.troll);
                    break;
                case 12:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.pixie);
                    break;
                case 13:
                    ((ImageView) view.findViewById(R.id.avatar)).setImageResource(R.drawable.half_orc);
                    break;
                default:
                    break;
            }
        }

        TextView textView = (TextView) view.findViewById(R.id.name);
        textView.setText(pcs[position]);
        view.setOnDragListener(new OnDragListener());
        view.setTag(c);
        return view;
    }

    private class OnDragListener implements View.OnDragListener {

        public boolean onDrag(View v, DragEvent event) {
            // Defines a variable to store the action type for the incoming event
            final int action = event.getAction();
            // Handles each of the expected events
            // returns false to tell we are not drop receiver
            switch (action) {
                case DragEvent.ACTION_DRAG_STARTED:
                        v.setAlpha(0.5f);
                    return false;
                case DragEvent.ACTION_DRAG_ENDED:
                        v.setAlpha(1f);
                    return true;
                // An unknown action type was received.
                default:
                    //Log.e("DragDrop Example","Unknown action type received by OnDragListener.");
                    break;
            };
            return false;
        }
    }
}
